﻿using Abp.Authorization;
using Abp.Extensions;
using Abp.Localization;
using AbpProjectTemplate.Components.Authorization;
using AbpProjectTemplate.Domain.Users;
using AbpProjectTemplate.Extensions;
using System;

namespace AbpProjectTemplate.Components
{
    public static class PermissionExtensions
    {
        /// <summary>
        /// 从定义中填充,如果DisplayName未定义，则使用和permission.name作为LocalizableString.Name
        /// </summary>
        public static void Fill(this Permission permission, PermissionDefine define)
        {
            define.ThrowUserFriendlyExceptionIfNull();
            permission.DisplayName = define.GetDisplayName();
            permission.MultiTenancySides = define.MultiTenancySides;
            permission[nameof(UserType)] = define.UserType;
        }
        /// <summary>
        /// 判断权限是否包含指定的用户类型
        /// </summary>
        public static bool HasUserType(this Permission permission, UserType userType)
        {
            return PermissionHelper.HasUserType(permission.Name, userType);
        }
    }
}
